package com.example.forum.services.impl;

import com.example.forum.common.AppResult;
import com.example.forum.common.ResultCode;
import com.example.forum.dao.MessageMapper;
import com.example.forum.exception.ApplicationException;
import com.example.forum.model.Message;
import com.example.forum.services.IMessageService;
import com.example.forum.utils.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description: 站内信的服务层实现类
 * User: DELL
 * Date: 2023-09-28
 * Time: 8:55
 */
@Slf4j
@Service
public class MessageServiceImpl implements IMessageService {

    @Resource
    private MessageMapper messageMapper;

    @Override
    public void create(Message message) {
        // 参数校验
        if (message == null || message.getPostUserId() == null || message.getReceiveUserId() == null
                || StringUtil.isEmpty(message.getContent())) {
            // 打印日志
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            // 抛出异常
            throw new ApplicationException(AppResult.fail(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        // 站内信的发起者和接收者不可以为同一用户
        if (message.getPostUserId() == message.getReceiveUserId()) {
            // 打印日志
            log.warn("站内信创建异常 -- 发起者和接收者为同一用户");
            // 抛出异常
            throw new ApplicationException(AppResult.fail("不可以自己给自己发送站内信"));
        }
        // 设置默认值
        message.setState((byte) 0); // 表⽰未读状态
        message.setDeleteState((byte) 0);
        Date date = new Date();
        message.setCreateTime(date);
        message.setUpdateTime(date);
        // 调用 dao
        int row = messageMapper.insertSelective(message);
        // 返回值检验
        if (row != 1) {
            // 打印日志
            log.warn("新增站内信异常," + message);
            // 抛出异常
            throw new ApplicationException(AppResult.fail(ResultCode.ERROR_SERVICES));
        }
    }

    @Override
    public int selectUnreadCount(Long userId) {
        // 参数校验
        if (userId == null || userId <= 0) {
            // 打印日志
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            // 抛异常
            throw new ApplicationException(AppResult.fail(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        // 调用 dao 层,查询并返回
        return messageMapper.selectUnreadCount(userId);
    }

    @Override
    public List<Message> selectByReceiveUserId(Long receiveUserId) {
        // 参数校验
        if (receiveUserId == null || receiveUserId <= 0) {
            // 打印日志
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            // 抛异常
            throw new ApplicationException(AppResult.fail(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        // 调用 dao,查询结果并返回
        return messageMapper.selectByReceiveUserId(receiveUserId);
    }

    @Override
    public void updateStateById(Long id, Byte state) {
        // 参数校验
        if (id == null || id <= 0 || state < 0 || state > 2) {
            // 打印日志
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            // 抛异常
            throw new ApplicationException(AppResult.fail(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        // 构造更新对象
        Message message = new Message();
        message.setId(id);
        message.setState(state);
        Date date = new Date();
        message.setUpdateTime(date);
        // 调用 dao
        int row = messageMapper.updateByPrimaryKeySelective(message);
        // 返回值检验
        if (row != 1) {
            // 打印日志
            log.warn("检测到更新站内信状态异常,message id = " + id + ", state = " + state);
            // 抛出异常
            throw new ApplicationException(AppResult.fail(ResultCode.ERROR_SERVICES));
        }
    }

    @Override
    public Message selectById(Long id) {
        // 参数校验
        if (id == null || id <= 0) {
            // 打印日志
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            // 抛异常
            throw new ApplicationException(AppResult.fail(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        // 调用 dao,查询结果并返回
        return messageMapper.selectByPrimaryKey(id);
    }

    @Override
    public void reply(Long repliedId, Message message) {
        // 参数校验
        if (repliedId == null || repliedId <= 0) {
            // 打印日志
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            // 抛异常
            throw new ApplicationException(AppResult.fail(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        // 更新对应的站内信状态
        updateStateById(repliedId, (byte) 2);
        // 新增站内信
        create(message);
    }
}
